﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Models;
using System.Text.RegularExpressions;
using System.Net.Mail;
using System.Text;

namespace CSMF
{
    public partial class three : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                int id = Convert.ToInt32(Request.QueryString["id"]);
                Models.VideoInfo info = VideoInfoManager.GetVideoInfoByID(id);
                litTitle.Text = info.Title;
                imgAuthor.ImageUrl = "UpLoad/videoImage/" + info.AuthorPhoto;
                litAuthor.Text = info.Author;
                litPoint.Text = info.Point.ToString();
                litDate.Text = info.UploadDate.ToString("yyyy.MM.dd");
                string neirong = Regex.Replace(Convert.ToString(info.VideoContent), "<[^>]*>", "").ToString();
                litVideoContent.Text = neirong.Length > 70 ? neirong.Substring(0, 69) + "..." : neirong;
                if (info.Count.ToString().Length == 4)
                {
                    litCount.Text = info.Count.ToString().Substring(0, 1) + "," + info.Count.ToString().Substring(1);
                }
                else
                {
                    litCount.Text = info.Count.ToString();
                }

                BindVideoLike(info.Author);
                BindVideoLast();
                BindVideo(id);

                VideoInfoManager.ModifyVideoInfoCount(id, (info.Count + 1));
            }
        }

        protected string GetPoint(int id)
        {
            string point = "";
            Models.VideoInfo info = VideoInfoManager.GetVideoInfoByID(id);
            if (info.Point == 0)
            {
                point = Convert.ToSingle((VideoPointManager.GetVideoPointsSumByVideoID(id) / VideoPointManager.GetVideoPointsCountByVideoID(id))).ToString();
                if (point.Equals("非数字"))
                {
                    point = "0";
                }
            }
            else
            {
                point = info.Point.ToString();
            }
            return point;
        }

        protected string GetVideo(int id)
        {
            int ids = Convert.ToInt32(Request.QueryString["id"]);
            string location;
            if (Request.QueryString["vid"] == null)
            {
                VideoLocation lo = VideoLocationManager.GetVideoLocationBytop1(ids);
                location = lo.Location;
            }
            else
            {
                int vid = Convert.ToInt32(Request.QueryString["vid"]);
                VideoLocation lo = VideoLocationManager.GetVideoLocationByID(vid);
                location = lo.Location;
            }
            return location;
        }

        protected void BindVideoLike(string author)
        {
            //IList<VideoInfo> video = VideoInfoManager.GetVideoInfosByAuthor(author);
            //rptVideoLike.DataSource = video;
            //rptVideoLike.DataBind();
        }

        protected void BindVideoLast()
        {
            IList<Models.VideoInfo> video = VideoInfoManager.GetVideoInfosLast();
            rptVideoLast.DataSource = video;
            rptVideoLast.DataBind();
        }

        protected void BindVideo(int id)
        {
            IList<VideoLocation> video = VideoLocationManager.GetVideoLocationsByVideoID(id);
            rptIcon.DataSource = video;
            rptIcon.DataBind();
        }

        protected string GetImage(int id)
        {
            Icon icon = IconManager.GetIconByID(id);
            return icon.Image;
        }


        protected void Button1_Click(object sender, EventArgs e)
        {
            int id = Convert.ToInt32(Request.QueryString["id"]);
            string ip = "";
            if (Context.Request.ServerVariables["HTTP_VIA"] != null) // 服务器， using proxy 
            {
                //得到真实的客户端地址
                ip = Context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();  // Return real client IP. 
            }
            else//如果没有使用代理服务器或者得不到客户端的ip  not using proxy or can't get the Client IP 
            {

                //得到服务端的地址 
                ip = Context.Request.ServerVariables["REMOTE_ADDR"].ToString(); //While it can't get the Client IP, it will return proxy IP. 
            }
            if (!VideoPointManager.GetVideoPointsByIP(ip, id))
            {
                VideoPoint po = new VideoPoint();
                po.IP = ip;
                po.Point = Convert.ToSingle(rblPoint.SelectedValue);
                po.VideoID = id;
                VideoPointManager.AddVideoPoint(po);
                ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "aaa", "alert('评分已提交。');", true);
            }
            else
            {
                ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "aaa", "alert('您已评过分了。');", true);
            }
        }

        protected void btnWarning_Click(object sender, EventArgs e)
        {
            string link = Request.Url.AbsoluteUri;
            string title = "微力十足-视频错误报告";
            StringBuilder sb = new StringBuilder();
            sb.Append("详细内容如下：<br><br>");
            sb.Append("报错内容：" + Convert.ToString(txtWarning.Text.Trim()) + "<br><br>");
            sb.Append("报错地址：" + link + "<br><br><br><br>");
            sb.Append("---------------------------------------------------------------------- <br><br>");
            sb.Append("(这是一封自动产生的email，请勿回复。) <br><br>");
            sb.Append("---------------------------------------------------------------------- <br><br>");
            SendMail(title, sb);

            ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "aaa", "alert('报告已发送。');", true);
        }


        ///<summary>
        ///发送邮件
        ///</summary>
        public void SendMail(string title, StringBuilder sb)
        {
            string mailfrom = "join@csmff.com";
            string mailto = "wyz@csmff.com";
            SmtpClient smtp = new SmtpClient();
            //smtp.UseDefaultCredentials = true;
            //smtp.Credentials = new NetworkCredential("join@csmff.com", "j2o0i1n2");
            MailMessage message = new MailMessage(mailfrom, mailto);
            message.IsBodyHtml = true;
            message.SubjectEncoding = System.Text.Encoding.UTF8;
            message.BodyEncoding = System.Text.Encoding.UTF8;
            message.Subject = title;
            message.Body = sb.ToString();
            smtp.Send(message);
            message.Dispose();
        }
    }
}
